package com.sl.hx.shop.ui.home.adapter;

import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.DynamicDrawableSpan;
import android.text.style.StrikethroughSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sl.hx.shop.R;
import com.sl.hx.shop.bean.ProductList;
import com.sl.hx.shop.utils.CornerTransform;
import com.sl.hx.shop.utils.ScreenUtil;

import java.util.List;


public class GoodsListAdapter extends BaseQuickAdapter<ProductList, BaseViewHolder> {


    public GoodsListAdapter(@Nullable List<ProductList> data) {
        super(R.layout.item_goods, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, final ProductList item) {

        //状态1 有效
        if (item.getStatus().equals("1")) {
            CornerTransform transformation = new CornerTransform(mContext, ScreenUtil.dp2px(mContext, 8));
            //只是绘制左上角和右上角圆角
            transformation.setExceptCorner(false, false, true, true);

            ImageView pic = helper.getView(R.id.pict);
            Glide.with(mContext)
                    .load(item.getPict_url())
                    .apply(RequestOptions.bitmapTransform(transformation).placeholder(R.drawable.homelist_bitmap))
                    .into(pic);

            helper.setText(R.id.zk_final_price, item.getZk_final_price().substring(0, item.getZk_final_price().lastIndexOf('.')))
                    .setText(R.id.volume, item.getVolume() + "人已付款");
            //.setText(R.id.coupon_info,item.getCoupon_info().substring(item.getCoupon_info().lastIndexOf("减"),item.getCoupon_info().lastIndexOf("元")));

            //满减优惠券
            TextView coupon_info = helper.getView(R.id.coupon_info);
            LinearLayout ll_coupon = helper.getView(R.id.ll_coupon);
            if (!TextUtils.isEmpty(item.getCoupon_info()) && !item.getCoupon_info().equals("")) {
                //截取数值
                String coupon = item.getCoupon_info().substring(item.getCoupon_info().lastIndexOf("减") + 1, item.getCoupon_info().lastIndexOf("元"));
                coupon_info.setText(coupon + "元券");
            } else {
                ll_coupon.setVisibility(View.GONE);
            }

            //标题前加淘宝图片
            TextView title = helper.getView(R.id.GoodsName);
            SpannableStringBuilder stringBuilder = new SpannableStringBuilder("  " + item.getTitle());
            DynamicDrawableSpan ab = new DynamicDrawableSpan() {
                @Override
                public Drawable getDrawable() {
                    if (item.getUser_type()==0) {
                        Drawable drawable = mContext.getResources().getDrawable(R.drawable.taobao);
                        drawable.setBounds(0, 0, 65, 36);
                        return drawable;
                    } else {
                        Drawable drawable = mContext.getResources().getDrawable(R.drawable.tianmao);
                        drawable.setBounds(0, 0, 65, 36);
                        return drawable;
                    }
                }
            };
            stringBuilder.setSpan(ab, 0, 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            title.setText(stringBuilder);

            //原价格设置删除线
            TextView reserve_price = helper.getView(R.id.reserve_price);
            SpannableStringBuilder spannableString = new SpannableStringBuilder("￥" + item.getReserve_price().substring(0, item.getReserve_price().lastIndexOf('.')));
            StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
            spannableString.setSpan(strikethroughSpan, 0, ("￥" + item.getReserve_price().substring(0, item.getReserve_price().lastIndexOf('.'))).length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
            reserve_price.setText(spannableString);
        }else {
            //状态0 无效商品
        }
    }

}
